#ifndef AXISCANVAS_H
#define AXISCANVAS_H

#include <QWidget>
#include <QMouseEvent>
#include <QResizeEvent>
#include <QWheelEvent>
#include <QKeyEvent>
#include <QPainter>
#include <QApplication>
#include <QDebug>

#include "axistiledmapview.h"
#include "axistiledmapscene.h"
#include "axissprite.h"

static inline void drawArrowLine(QPainter* painter, int sx, int sy, int ex, int ey, QColor c, QString text = "")
{
    QPen pen = QPen();
    pen.setColor(c);
    pen.setWidth(1);
    pen.setJoinStyle(Qt::MiterJoin);
    painter->setPen(pen);

    // setBrush
    QBrush brush = QBrush();
    brush.setColor(c);
    brush.setStyle(Qt::SolidPattern);
    painter->setBrush(brush);

    QLineF line(sx, sy, ex, ey);
    QLineF v = line.unitVector();
    v.setLength(5);
    v.translate(QPointF(line.dx(), line.dy()));

    QLineF n = v.normalVector();
    n.setLength(n.length() * 0.5);
    QLineF n2 = n.normalVector().normalVector();

    QPointF p1 = v.p2();
    QPointF p2 = n.p2();
    QPointF p3 = n2.p2();
    QPolygonF plygonf;
    plygonf << p1 << p2 << p3;

    painter->drawLine(line);
    painter->drawPolygon(plygonf);
    painter->drawText(p2, text);

}

class AsixTickMarkWidget : public QWidget
{
public:
    AsixTickMarkWidget(QWidget* parent = 0)
        : QWidget (parent)
    {
        setAttribute(Qt::WA_TranslucentBackground, true);
        setMouseTracking(true);
    }

    void paintEvent(QPaintEvent* event)
    {
        QWidget::paintEvent(event);

        QPainter painter(this);

        QPen pen;
        pen.setColor(QColor(255, 255, 255));
        painter.setPen(pen);

        int wmax = width();
        int hmax = height();
        for( int h = 0; h < wmax; ++h )
        {
            int ext = 0;
            if ( 0 == h % 5 )
                ext = 5;
            painter.drawLine(0, h * 5, 5 + ext, h * 5);
        }
        for( int w = 0; w < hmax; ++w )
        {
            int ext = 0;
            if ( 0 == w % 5 )
                ext = 5;
            painter.drawLine(w * 5, 0, w * 5, 5 + ext);
        }
        drawArrowLine(&painter, 15, 15, 15, 100, Qt::green, "y");
        drawArrowLine(&painter, 15, 15, 100, 15, Qt::red, "x");
        int ox = width() - 60;
        int oy = 60;
        int leftx = ox - 32;
        int lefty = oy + 16;
        int rightx= ox + 32;
        int righty= oy + 16;
        drawArrowLine(&painter, ox, oy, leftx, lefty, Qt::green);
        drawArrowLine(&painter, ox, oy, rightx, righty, Qt::red);
        drawArrowLine(&painter, ox, oy, ox, oy - 32, Qt::blue);
    }

    void mousePressEvent(QMouseEvent* e){ postMouseEventToSiblings(e); }
    void mouseReleaseEvent(QMouseEvent* e){ postMouseEventToSiblings(e);setFocus(); }
    void mouseMoveEvent(QMouseEvent* e){ postMouseEventToSiblings(e); }
    void postMouseEventToSiblings(QMouseEvent *e)
    {
        if(this->parentWidget())
        {
            this->setAttribute(Qt::WA_TransparentForMouseEvents, true);
            QPoint pt = this->mapTo(this->parentWidget(), e->pos());
            QWidget *w = this->parentWidget()->childAt(pt);
            if (w)
            {
                pt = w->mapFrom(this->parentWidget(), pt);
                QMouseEvent *event = new QMouseEvent(e->type(), pt, e->button(),
                e->buttons(), e->modifiers());
                QApplication::postEvent(w, event);
            }
            this->setAttribute(Qt::WA_TransparentForMouseEvents, false);
        }
    }

};

class AxisCanvas : public QWidget
{
public:
    AxisCanvas(QWidget* parent = 0);

    void resizeEvent(QResizeEvent* event);
    void mousePressEvent(QMouseEvent* event);
    void mouseReleaseEvent(QMouseEvent* event);
    void mouseMoveEvent(QMouseEvent* event);
    void wheelEvent(QWheelEvent *event);
    void keyPressEvent(QKeyEvent* event);
    void keyReleaseEvent(QKeyEvent* event);

    AxisSprite* createMapObject(int w, int h);
    void addMapObject(AxisSprite* m, int gridx, int gridy);

    void zoomIn();  // 放大
    void zoomOut();  // 缩小
    void zoom(qreal scaleFactor); // 缩放 - scaleFactor：缩放的比例因子

protected:
    qreal _scale = 0.0;
    bool _rightbtnclicked = false;
    QPointF _point;
    AsixTickMarkWidget* _tmwidget = nullptr;
    AxisTiledMapView _view;
    AxisTiledMapScene _scene;
    bool _ctrlpress = false;

};

#endif // AXISCANVAS_H
